package com.crm.jdy.service.drp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.crm.jdy.common.dto.HDepartmentInfoDto;
import com.crm.jdy.common.mapper.FRecordInfoMapper;
import com.crm.jdy.common.pojo.HDepartmentInfo;
import com.crm.jdy.common.mapper.HDepartmentInfoMapper;
import com.crm.jdy.common.pojo.TeamInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**   
 * @Description:(部门表信息服务实现)
 *
 * @version: V1.0
 * @author: kong
 * 
 */
@Service(version = "${service.version}")
@Component
public class HDepartmentInfoServiceImpl  extends ServiceImpl<HDepartmentInfoMapper,HDepartmentInfo> implements HDepartmentInfoService  {

    @Autowired
    HDepartmentInfoMapper hDepartmentInfoMapper;

    @Override
    public PageInfo<HDepartmentInfo> seleteHDepartmentInfo(HDepartmentInfoDto wOutInfo) {
        PageHelper.startPage( wOutInfo.getCurrentPage(),wOutInfo.getPageSize());
        List<HDepartmentInfo> bookList = hDepartmentInfoMapper.seleteHDepartmentInfo(wOutInfo);
        PageInfo<HDepartmentInfo> pageInfo = new PageInfo<HDepartmentInfo>(bookList);
        return pageInfo;
    }

    @Override
    public boolean updateHDepartmentInfo(HDepartmentInfo wOutInfo) {
        return hDepartmentInfoMapper.updateById(wOutInfo) > 0;
    }

    @Override
    public boolean addHDepartmentInfo(HDepartmentInfo wOutInfo) {
        return hDepartmentInfoMapper.insert(wOutInfo) > 0;
    }

    @Override
    public boolean removeHDepartmentInfo(String deptId) {
        return hDepartmentInfoMapper.deleteById(deptId) > 0;
    }


//  组织架构图
    @Override
    public List<HDepartmentInfoDto> seleteOrganizationInfo() {
        System.out.println("---zhixingel-------");
//      查询所有的目录信息
        List<HDepartmentInfo> data = hDepartmentInfoMapper.selectList(null);
        System.out.println("------data------");
        System.out.println(data);
        // 用boot获取分类的数据   selectAll在springboot中是获取数据表里的所有数据
//        List<HDepartmentInfoDto> data = categoryMapper.selectAll();
        //定义新的list
        List<HDepartmentInfoDto> categoriesList = new ArrayList<>();
        //先找到所有的一级分类
        for(HDepartmentInfo  category : data){
            // 一级菜单的parentId是0
            if(category.getDeptParent().equals("0")){
                System.out.println(category.getDeptParent()+"上级目录");
                HDepartmentInfoDto  hDepartmentInfoDto =new HDepartmentInfoDto();
                BeanUtils.copyProperties(category,hDepartmentInfoDto);
                categoriesList.add(hDepartmentInfoDto);    // 获取第一级的目录信息
            }
        }
        // 为一级菜单设置子菜单，getChild是递归调用的
        for(HDepartmentInfoDto category : categoriesList){
            category.setTreeList(getChilde(category.getDeptId(), data));
        }

        return categoriesList;
    }

    /**
     * 递归查找子菜单
     * @param id 当前菜单id
     * @param rootList 要查找的列表
     * @return
     */
    private List<HDepartmentInfoDto> getChilde(String id, List<HDepartmentInfo> rootList){
        //子菜单
        List<HDepartmentInfoDto> childList = new ArrayList<>();   //获取新的目录信息
        for(HDepartmentInfo category : rootList){
            // 遍历所有节点，将父菜单id与传过来的id比较
            if(category.getDeptParent().equals(id)){
                HDepartmentInfoDto  hDepartmentInfoDto =new HDepartmentInfoDto();
                BeanUtils.copyProperties(category,hDepartmentInfoDto);
                childList.add(hDepartmentInfoDto);
            }
        }
        // 把子菜单的子菜单再循环一遍
        for(HDepartmentInfoDto category : childList){
            category.setTreeList(getChilde(category.getDeptId(), rootList));
        }
        // 递归退出条件
        if (childList.size() == 0){
            return null;
        }
        return childList;
    }


}